package vip.xiaonuo.modular.reactor;

import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.Reactor;
import reactor.event.Event;
import reactor.function.Consumer;
import vip.xiaonuo.facedoor.modular.topic.EventTopic;
import vip.xiaonuo.facedoor.modular.visitor.entity.Visitor;
import vip.xiaonuo.facedoor.modular.visitor.service.VisitorService;
import vip.xiaonuo.modular.remoteapi.stairs.RemoteStairsConstant;
import vip.xiaonuo.modular.sync.DeviceEvent;
import vip.xiaonuo.modular.ybuildingvisitorrl.entity.YBuildingVisitorRl;
import vip.xiaonuo.modular.ybuildingvisitorrl.mapper.YBuildingVisitorRlMapper;
import vip.xiaonuo.modular.ybuildingvisitorrl.param.YBuildingVisitorRlParam;
import vip.xiaonuo.modular.ydeviceevent.entity.YDeviceEvent;
import vip.xiaonuo.modular.ydeviceevent.mapper.YDeviceEventMapper;
import vip.xiaonuo.modular.yelevatordevice.entity.YElevatorDevice;
import vip.xiaonuo.modular.yelevatordevice.service.YElevatorDeviceService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static reactor.event.selector.Selectors.$;

/**
 * 梯控监听访客删除事件做人员变更
 */
@Service
@Slf4j
public class StarisVisitorDeviceDeleteListener implements Consumer<Event<List<Long>>>, InitializingBean {

    @Resource
    private YBuildingVisitorRlMapper yBuildingVisitorRlMapper;

    @Autowired
    private YElevatorDeviceService elevatorDeviceService;

    @Resource
    private YDeviceEventMapper deviceEventEntityMapper;


    @Autowired
    private Reactor reactor;

    @Autowired
    private VisitorService visitorService;

    @Override
    public void accept(Event<List<Long>> event) {
        for (Long visitorId : event.getData()) {
            Visitor visitor = visitorService.getById(visitorId);
            if (visitor == null) {
                log.error("访客不存在！");
                continue;
            }
            if (ObjectUtil.equal(visitor.getIssuanceOfVouchers(),1)){
                log.info("【{}】未下发凭证 直接清退！",visitor.getName());
                continue;
            }
            List<YBuildingVisitorRl> list = yBuildingVisitorRlMapper.findList(new YBuildingVisitorRlParam().setVisitorId(visitorId));
            if (CollectionUtils.isNotEmpty(list)) {
                for (YBuildingVisitorRl yBuildingVisitorRl : list) {
                    // 新增中 直接删除
                    if (ObjectUtil.equal(yBuildingVisitorRl.getDeviceSyncStatus(),RemoteStairsConstant.DeviceSyncStatus.ADD)){
                        yBuildingVisitorRlMapper.deleteById(yBuildingVisitorRl);
                        continue;
                    }
                    // 未发卡 直接删除
                    if (StringUtils.isBlank(visitor.getIcCard())){
                        yBuildingVisitorRlMapper.deleteById(yBuildingVisitorRl);
                        continue;
                    }
                    // 删除中 不做操作
                    if (ObjectUtil.equal(yBuildingVisitorRl.getDeviceSyncStatus(),RemoteStairsConstant.DeviceSyncStatus.DELETE)){
                        continue;
                    }
                    // 如果当前状态为新增失败 直接删除
                    if (ObjectUtil.equal(yBuildingVisitorRl.getDeviceSyncStatus(),RemoteStairsConstant.DeviceSyncStatus.SYNC_ERROR)){
                        YDeviceEvent errorEventEntity = deviceEventEntityMapper.selectByBody(yBuildingVisitorRl.getId().toString());
                        if (errorEventEntity != null) {
                            if (errorEventEntity.getType().equals(RemoteStairsConstant.RequestCmd.ADD_PERSON)) {
                                yBuildingVisitorRlMapper.deleteById(yBuildingVisitorRl);
                                continue;
                            }
                        }
                    }
                    yBuildingVisitorRl.setDeviceSyncStatus(RemoteStairsConstant.DeviceSyncStatus.DELETE);
                    yBuildingVisitorRlMapper.updateById(yBuildingVisitorRl);
                    // 存在任务表
                    YElevatorDevice device = elevatorDeviceService.getById(yBuildingVisitorRl.getDeviceId());
                    YDeviceEvent deviceEvent = new YDeviceEvent();
                    deviceEvent.setDeviceNo(device.getDeviceNo());
                    deviceEvent.setType(RemoteStairsConstant.RequestCmd.DELETE_PERSON);
                    deviceEvent.setCreateDate(new Date());
                    deviceEvent.setSysType(2);
                    deviceEvent.setBody(yBuildingVisitorRl.getVisitorId() + ":" + yBuildingVisitorRl.getVisitorId());
                    deviceEvent.setStatus(DeviceEvent.NEW);
                    deviceEventEntityMapper.insert(deviceEvent);
                }
            }
        }
    }

    @Override
    public void afterPropertiesSet(){
        reactor.on($(EventTopic.VISITOR_DEVICE_DELETE), this);
    }
}
